list
A list is a row of items where the number of items changes as the game runs. A pickup goes into the inventory, a used potion comes out, a new score joins a table. You add to the end, you remove from anywhere, and at any moment you can ask how many items are in it.
That is the difference from the array, the container it is closest to. An array has a fixed number of slots that never change. A list starts empty (or with a few items you give it) and gains and loses items over time. Both are read the same way, with [i], where i is the item's index: its position in the row, counting from 0. inventory[0] is the first item, inventory[1] the second.
Everything a list can do is on this page: fifteen functions and [i]. Pick a list when the order of the items matters and the count is not known ahead of time - an inventory, a high-score table, "the last few things that happened."
There is no list_get or list_set, because [i] already does both: inventory[2] reads the third item and inventory[2] = "key" changes it. The functions are for the things [i] cannot do
/// @type {list<string>}
/// @capacity 6
var inventory = ["sword"];The /// @type line says what the list holds. list<string> is a list where every item is a piece of text; list<int> would hold whole numbers, list<ref<obj_enemy>> would hold references to enemies. Every item in one list has the same type. To keep several differently-typed values together in one item, see Structs in data containers.
The /// @capacity line says the most items the list can ever hold. Leave it off and you get 8. It can be any number: unlike the queue, map and set, a list has no rule about which sizes are allowed. The limit exists because the GBA has no spare memory to grow into. The list's whole space is set aside when the game is built, so the size has to be a real number you picked. An object that keeps a list in one of its variables carries its own copy of that space, so a large capacity on an object with many instances adds up.
The items in brackets after = are the starting contents. Start empty with []. A starting list with more items than the capacity is refused when the project is checked, before the game runs.
The same list can be written with no comment at all. Both of these are the list above, just empty:
var inventory: list<string, 6> = []; // the type, with the size inside it
var inventory = list_create(6, ""); // a function: capacity, then an examplelist_create's second value is only an example of what the list holds. It is never put in the list, which starts empty. A list that is never given a size - by a /// @capacity comment, a number in its type, or list_create - holds 8 items. The comment form and the written-out forms are all equal; see Writing a type out.
To make a list that starts full instead of empty, use list_create_fill: var names = list_create_fill(12, "HI"); is a list with room for 12 that already holds twelve "HI". Its value is both the example (it sets the kind of list) and the thing that is put in every place. It is list_create and list_fill in one line. Only the list has one: array_create and grid_create already start full.
list_length is how many items are in the list right now. The capacity is the most it could hold. A list with capacity 6 and two items has a length of 2.
The two matter together when you use [i]. A list only has a slot for each item that has been added, so [i] works for indexes from 0 up to the length minus 1. To give a list all its slots at once, so [i] works for every index up to the capacity, use list_fill. Reading or writing inventory[3] when the list holds only two items is a mistake: The Game Boy Advance module, which the game is built on, checks every index and stops the game with a message naming the bad one. That check is on by default.
| Function | What it does | It gives back |
|---|---|---|
list_add(list, value) | puts value at the end | true if it fit, false if the list was full |
list_insert(list, index, value) | puts value at index and moves the rest along | true if it went in, false if index was not a place it could go |
list_remove(list, index) | takes out the item at index | true if it removed something, false if index was out of range |
list_pop(list) | takes out the LAST item | that item (or a plain 0, false, empty text or nothing if the list was empty) |
list_length(list) | counts the items | the count |
list_clear(list) | empties the list | nothing |
list_fill(list, value) | grows the list to its capacity, every place holding value | nothing |
list_contains(list, value) | looks for value | true or false |
list_find(list, value) | looks for value and says where it is | its place, or -1 if it is not there |
list_reverse(list) | turns the order of the items around | nothing |
list_copy(list) | makes a second list just like it, with the same room | the new list |
list_create_fill(capacity, value) | makes a list that already holds capacity copies of value | the new list |
list_sort(list, ascending) | puts the numbers or the text in order, smallest (A) first or biggest (Z) first | nothing |
list_shuffle(list) | mixes up the order of the items | nothing |
list_foreach(list, script) | runs one of your scripts once for each item | nothing |
list_filter(list, script) | makes a new list of just the items a script says yes to | the new list |
list_keep(list, script) | takes out of the list itself the items a script says no to | nothing |
list_sort_by(list, script, ascending) | puts the items in order by a number a script works out for each one (works on structs and references too) | nothing |
list_max(list), list_min(list), list_sum(list) | the biggest number, the smallest number, or the total of a list of numbers | a number |
A few details that decide how you use them:
list_contains and list_find look at every item. They read from the front until they find a match, so they take longer the longer the list is. list_find gives the place of the first match, and -1 when the value is not in the list - test it with >= 0 before using it as a place. If a list is mostly there so you can ask "is this in here," a set answers the same question in about the same time whatever its size.list_insert and list_remove count places the same way. 0 is the first place, so list_insert(items, 2, x) puts x where items[2] will be, and list_remove(items, 2) takes it out again.A list is a box of a fixed size, and a GBA has no spare memory to make it bigger while the game runs. So when a list is full and something more is put in, one item has to be left out. Neither function crashes or stops the game, and the rule is the same in both: whatever would end up past the end is lost.
| You do this to a full list | What happens |
|---|---|
list_add(items, x) | x is not added, everything already there stays, and you get false |
list_insert(items, i, x) where i is before the end | x goes in at i, the last item falls off the end and is lost, and you get true |
list_insert(items, list_length(items), x) (at the very end) | nothing changes, because x is the one that would be past the end, and you get false |
So list_add protects what is already in the list, and list_insert protects the new item. This is on purpose, not a bug to work around. If losing the last item would matter, ask first with list_length: compare it to the size you gave the list, and only insert when there is room. Ignoring the true/false answer is fine otherwise; look at it only when the game needs to react, such as showing "inventory full."
The other containers are the same kind of safe, but they are not all the same rule. The "When a container is full" section of Data containers lines them up.
To run one of your scripts on each item in turn, use list_foreach. To do something else with each item, use a for loop with the length and [i]:
/// @type {list<int>}
var scores = [10, 20, 30];
var total = 0;
for (var i = 0; i < list_length(scores); i++) {
total += scores[i];
}i starts at 0 and goes up by one until it reaches the length, so the loop body runs once for every item, and never touches an index that does not exist. This is also the way to do anything the sixteen functions do not: there is no function to sort a list of anything but numbers and text, or to find every place a value is at (list_find stops at the first), so those are written as loops over [i].
Four of the sixteen change the whole list at once: list_sort puts a list of numbers or of text in order, list_shuffle mixes up a list of anything, list_reverse turns the order of a list of anything around, and list_keep takes out the items a script says no to. All four work on the list itself and make no copy, so use list_copy first if you want to keep the old list. list_filter leaves the list alone and always gives back a new one, because how many items pass is not known until the game runs; it costs a whole second list, which list_keep does not.
/// @type {list<string>}
/// @capacity 4
var inventory = ["sword"];
list_add(inventory, "shield");
list_add(inventory, "potion");
var count = list_length(inventory); // 3
var first = inventory[0]; // "sword"
list_remove(inventory, 0); // "shield" is now first
var has_potion = list_contains(inventory, "potion"); // trueWhen the list is full, list_add says so:
/// @type {list<int>}
/// @capacity 2
var slots = [1, 2];
var fit = list_add(slots, 3); // false - slots is full, and is unchangedA script that takes a list as a parameter (see Functions) works on the caller's own list, not on a copy. Adding to it or removing from it inside the script changes the list in the object that called it. Every container works this way, and it is why a script can write list_add(items, x) with no need to hand the list back.
Reach for a list when items come and go and you want to keep their order. Reach for an array when the count never changes. If items should only ever be added and taken from one end, a stack (last in, first out) or a queue (first in, first out) says so in the code, and the queue never shifts items the way removing from the front of a list does. If you look things up by a name instead of a position, use a map. Data containers compares all seven side by side.